package com.soft.admin.service.impl;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.soft.admin.service.ActionService;
import com.soft.model.Action;
import com.soft.support.PageParam;
import com.soft.support.Pager;
import com.soft.util.JsonUtil;

@SuppressWarnings("unchecked")
@Component
public class ActionServiceImpl extends BaseServiceImpl implements ActionService {
	
	@Transactional(readOnly = true)
	public String get(Action params, PageParam page) {
		Pager actions = this.getBaseDao().findForPager(Action.class, new Order[] { Order.desc("id") },
				page.getStart(), page.getLimit(),
				Restrictions.like("code", params.getCode(), MatchMode.ANYWHERE),
				Restrictions.like("url", params.getUrl(), MatchMode.ANYWHERE));
		return JsonUtil.toStringFromObject(actions);
	}

	
	@Transactional
	public String addOrEdit(Action params) {
		Action action = null;
		if (params.getId() == null) {
			action = new Action();
		} else {
			action = (Action)this.getBaseDao().get(Action.class, params.getId());
		}
		action.setCode(params.getCode());
		action.setUrl(params.getUrl());
		action.setRemark(params.getRemark());
		this.getBaseDao().save(action);
		return JsonUtil.toRes("保存成功！");
	}

	@Transactional
	public String remove(Action params) {
		this.getBaseDao().delete(Action.class, params.getId());
		return JsonUtil.toRes("删除成功！");
	}

}
